import { observable, action } from "mobx";
import { each } from 'lodash';
import { message } from 'antd';
import u from "../utils";
import API from '../apis/cuspath';
import NodeFrom from "./entity/NodeForm";
import { displayTypes } from "../constants/app";
import { TaskViewPageType } from "../constants/task";
import CombineSetDialogStore from "./dialog/combineSetDialogStore";
import FieldPropDialogStore from './dialog/FieldPropDialogStore';
import TemplateBrowserStore from "./dialog/templateBrowserStore";
import OperateMenuDialogStore from "./dialog/operateMenuDialogStore";
import { getPathByAppId } from "../apis/path";
import NodeModalStore from "./dialog/NodeModalStore";
const _API = u.api('custompath');
import {WeaLocaleProvider} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export default class PathDesignerStore {
    @observable data;
    @observable versionNode;
    @observable node;
    @observable path;
    @observable showRight = true;
    @observable rightLoading = false;
    @observable readonly = false;
    @observable nodeForm = new NodeFrom();
    @observable childOpreator = [];

    @observable combine = '1';
    @observable layout = '0';
    @observable scale = 1;

    @observable pathType = 0;
    @observable dynamicNode;
    @observable hasChildren = false;


    @observable leftWidth = 3;
    @observable rightWidth = 6;

    @observable combineSetDialogStore;
    @observable fieldPropDialogStore;
    @observable templateBrowserStore = new TemplateBrowserStore('custompath');
    @observable nodeModalStore = new NodeModalStore();

    @observable operateMenuDialogStore;

    constructor() {
        this.nodeForm.parent = () => this;
        this.templateBrowserStore.title = getLabel('503349','选择路径');
        this.combineSetDialogStore = new CombineSetDialogStore();
        this.fieldPropDialogStore = new FieldPropDialogStore(this);
        this.operateMenuDialogStore = new OperateMenuDialogStore(this);
        message.config({top: 60});
    }
    @action hideNodeForm = () => {
        this.showRight = false;
    }
    @action changeLayout = (layout) => {
        this.layout = layout;
    }
    @action changeScale = (scale) => {
        this.scale = scale;
    }
    @action
    addNodes = (nodeid, ids, img) => {
        _API('addCustomNodes', { type: "post", data: { nodeid, ids, pathid: this.path.id, hreflink: JSON.stringify(this.nodeForm.defaultPage), img } })
            .then((path) => {
                this.setPath(path);
                this.hasChildren = true;
                message.success(getLabel('502231','保存成功'));
            });
    }
    @action allChildOpreator = (nodeid) => {
        this.childOpreator = [];
        return API.allChildOpreator(nodeid);
    }
    @action setChildOpreator = (childOpreator) => {
        this.childOpreator = childOpreator;
    }
    @action saveAsTemplate = (img) => {
        API.saveAsTemplate({ pathid: this.path.id, img }).then(() => {
            message.success(getLabel('502231','保存成功'));
        });
    }
    @action loadTemplate = ({ id = 0 } = {}) => {
        API.loadTemplate({ id, pathid: this.path.id, hreflink: JSON.stringify(this.nodeForm.defaultPage) }).then(this.setPath);
    }
    @action
    deleteNode = (nodeid) => {
        _API('deleteCustomNode', { data: { nodeid } }).then(this.afterDelete);
    }
    @action
    deleteChildren = (nodeid) => {
        API.deleteChildren(nodeid).then((path) => {
            this.setPath(path);
            this.hasChildren = false;
            this.setChildOpreator([])
            message.success(getLabel('502230','删除成功'));
        });
    }
    @action afterDelete = (path) => {
        this.setPath(path);
        this.nodeForm = new NodeFrom();
        this.nodeForm.parent = () => this;
        this.showRight = false;
        message.success(getLabel('502230','删除成功'));
    }
    @action
    saveNode = () => {
        const nodeFormParams = this.nodeForm.toJSON();
        if(nodeFormParams.synchronizeType==='2'&&nodeFormParams.synchronizeNodeId===null){
            message.error("未指定同步节点");
            return;
        }
        API.saveNode(nodeFormParams).then((path) => {
            message.success(getLabel('502231','保存成功'));
            this.setPath(path);
        });
    }
    @action
    openNode = (node, hasChildren) => {
        this.showRight = true;
        this.rightLoading = true;
        this.nodeForm.reportTypeVisible = hasChildren;
        this.hasChildren = hasChildren;
        API.versionNode(node.id).then(this.initNodeForm)
    }
    @action
    initNodeForm = (node) => {
        this.rightLoading = false;
        if (this.parent().app.displayType === displayTypes.FORM) {
            this.nodeForm.hreflinkObjLabel = getLabel('502896','表单');
            this.nodeForm.hreflinkObjOptions = this.nodeForm.hreflinkObjOptions.filter(d => d.key !== TaskViewPageType.EXCEL);
            this.nodeForm.defaultPage = [TaskViewPageType.FORM];
        } else {
            this.nodeForm.hreflinkObjLabel = getLabel('502895','表格');
            this.nodeForm.defaultPage = [TaskViewPageType.EXCEL, '', this.parent().selectedKey];
            this.nodeForm.hreflinkObjOptions = this.nodeForm.hreflinkObjOptions.filter(d => d.key !== TaskViewPageType.FORM);
        }
        this.nodeForm.init(node, this.parent().excelPages);

    }
    @action
    initPath = (params, readonly) => {
        this.params = params;
        this.readonly = readonly;
        /*  _API('initCustomPath', { data: params }).then((path) => {
             this.setPath(path);
             this.openNode(this.data, true);
         }); */
        getPathByAppId(params).then(path => {
            this.setPath(path);
            //this.openNode(this.data, true);
        });
    }

    @action
    setPath = (path) => {
        this.combine = path.combine;
        this.node = path.node;
        this.versionNode = path.versionNode;
        this.pathType = path.pathType;
        if(path.pathType&&path.pathType===2) this.dynamicNode = path.dynamicNode;
        delete path.node;
        delete path.versionNode;
        this.path = path;
        // this.openNode(this.data, this.nodeForm&&this.nodeForm.reportTypeVisible);
    }

    @action toggleCombine = (combine) => {
        this.combine = combine;
        API.toggleCombine({ pathid: this.path.id, combine });
    }

    @action openPathTemplateDialog = () => {
        this.templateBrowserStore.open();
    }


    @action fixedWidth = (clientWidth) => {
        const all = this.leftWidth + this.rightWidth;
        const per = clientWidth / all;
        this.leftWidth = per * this.leftWidth;
        this.rightWidth = per * this.rightWidth;
    }
    @action onLeftResize = (e, { size: { width } }) => {
        this.rightWidth = this.leftWidth + this.rightWidth - width;
        this.leftWidth = width;
        this.treeRef.resize();
    }

    @action showNodeModal = (id, parentid) => {
        if(id) {
            this.nodeModalStore.init(id,parentid)
            this.nodeModalStore.title = getLabel('503350','设置节点');
            
        }
        this.nodeModalStore.open();
    }

    @action synchronizeNode = (ids) => {
        API.synchronizeNode({nodeid:this.nodeForm.id,synchronizeNodeId:ids}).then((path) => {
            message.success(getLabel('502231','保存成功'));
            //this.setPath(path);
        });
    }

}